package com.cssw.fyzb.core.dao.acct;

import com.cssw.fyzb.core.model.dodb.*;
import com.cssw.fyzb.core.util.TimeUtil;
import org.springframework.stereotype.Repository;
import com.ydn.dbframe.plugin.activerecord.Db;
import com.ydn.dbframe.plugin.activerecord.Page;
import com.ydn.dbframe.plugin.activerecord.Record;
import com.ydn.dbframe.plugin.activerecord.parse.SqlParse;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author zhouxu
 * @create 2020-12-25 16:34
 */
@Repository
public class UserCashOutDaoImpl implements UserCashOutDao {
    @Override
    public Long addCashOut(Long userId, int cashOutRatio, BigDecimal cashOutFee, BigDecimal handlingFee, BigDecimal cashAmount, String cashAcctType, Long acctId, String acctType) {
        return addCashOut(userId, cashOutRatio, cashOutFee, handlingFee, cashAmount, cashAcctType, acctId, acctType, UserCashOut.STATUS_INIT);
    }

    @Override
    public Long addCashOut(Long userId, int cashOutRatio, BigDecimal cashOutFee, BigDecimal handlingFee, BigDecimal cashAmount, String cashAcctType, Long acctId, String acctType, String status) {
        UserCashOut cashOut = new UserCashOut();
        cashOut.setUserId(userId);
        cashOut.setAcctId(acctId);
        cashOut.setAcctType(acctType);
        cashOut.setCashOutRatio(cashOutRatio);
        cashOut.setCashAmount(cashAmount);
        cashOut.setCashAcctType(cashAcctType);
        cashOut.setCashOutFee(cashOutFee);
        cashOut.setHandlingFee(handlingFee);
        cashOut.setCashOutRatio(cashOutRatio);
        Date createTime = new Date();
        int dayTime = TimeUtil.getTimeYYMMDD(createTime);
        cashOut.setDayTime(dayTime);
        cashOut.setCreateTime(createTime);
        cashOut.setStatus(status);
        cashOut.save();
        return cashOut.getCashId();
    }

    @Override
    public void updateDrawcashId(Long cashId, String drawcashId) {
        UserCashOut cashOut = new UserCashOut();
        cashOut.setCashId(cashId);
        cashOut.setDrawcashId(drawcashId);
        cashOut.update();
    }

    @Override
    public void updateCashStatus(Long cashId, String cashStatus) {
        UserCashOut cashOut = new UserCashOut();
        cashOut.setCashId(cashId);
        cashOut.setCashStatus(cashStatus);
        cashOut.update();
    }

    @Override
    public void updateCashError(Long cashId, String cashStatus, String errorCode, String errorMsg) {
        UserCashOut cashOut = new UserCashOut();
        cashOut.setCashId(cashId);
        cashOut.setCashStatus(cashStatus);
        cashOut.setErrorMsg(errorMsg);
        cashOut.setErrorCode(errorCode);
        cashOut.update();
    }


    @Override
    public void saveCashOutLog(Long cashId, String status, String cashStatus, String remark) {
        UserCashOutLog userCashOutLog = new UserCashOutLog();
        userCashOutLog.setCashId(cashId);
        userCashOutLog.setStatus(status);
        userCashOutLog.setRemark(remark);
        userCashOutLog.setCreateTime(new Date());
        userCashOutLog.save();
    }

    @Override
    public UserCashOut getByDrawcashId(String drawcashId) {
        String sql = "select * from t_user_cash_out where drawcash_id = ?";
        return UserCashOut.dao.findFirst(sql, drawcashId);
    }

    @Override
    public void updateStatus(Long cashId, String status) {
        UserCashOut userCashOut = new UserCashOut();
        userCashOut.setCashId(cashId);
        userCashOut.setStatus(status);
        userCashOut.setFinishTime(new Date());
        userCashOut.update();
    }

    public Page<Record> list(int page, int limit, Map param){
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL(" SELECT a.*,b.nick_name,b.mobile ");
        sqlParse.addSQL(" FROM t_user_cash_out a LEFT JOIN t_user b ON a.user_id=b.user_id  ");
        sqlParse.addSQL(" WHERE 1=1 ");
        sqlParse.addLikeSQL(" AND (b.nick_name LIKE :search ");
        sqlParse.addLikeSQL(" OR mobile LIKE :search) ");
        sqlParse.addSQL(" ORDER BY `status`,cash_id DESC ");
        return Db.paginate(page, limit, sqlParse);
    }


    @Override
    public void addCashOutAli(Long cashId, String aliNo, String realName, String receQrCode) {
        UserCashOutAli cashOutAli = new UserCashOutAli();
        cashOutAli.setCashId(cashId);
        cashOutAli.setAliNo(aliNo);
        cashOutAli.setRealName(realName);
        cashOutAli.setReceQrCode(receQrCode);
        cashOutAli.save();
    }

    @Override
    public void addCashOutWechat(Long cashId, String wechatNo, String realName, String receQrCode) {
        UserCashOutWechat cashOutWechat = new UserCashOutWechat();
        cashOutWechat.setCashId(cashId);
        cashOutWechat.setWechatNo(wechatNo);
        cashOutWechat.setRealName(realName);
        cashOutWechat.setReceQrCode(receQrCode);
        cashOutWechat.save();
    }

    @Override
    public void addCashOutBank(Long cashId, Long bankId, String cardNo, String realName, String bankName, String openBank, String bankCode, String provCode, String cityCode) {
        UserCashOutBank cashOutBank = new UserCashOutBank();
        cashOutBank.setCashId(cashId);
        cashOutBank.setBankId(bankId);
        cashOutBank.setCardNo(cardNo);
        cashOutBank.setRealName(realName);
        cashOutBank.setBankName(bankName);
        cashOutBank.setOpenBank(openBank);
        cashOutBank.setBankCode(bankCode);
        cashOutBank.setProv(provCode);
        cashOutBank.setCity(cityCode);
        cashOutBank.save();
    }

    @Override
    public Integer countCurrMonthGzCashNum(Long userId, Date firstDate, Date lastDate, String acctType) {
        String sql = "select count(1) from t_user_cash_out where user_id = ? and status in ('1', '2') and create_time >= ?  and create_time <= ? and acct_type = ?";
        return Db.queryInt(sql, userId, firstDate, lastDate, acctType);
    }

    @Override
    public BigDecimal sumCurrMonthGzCashFee(Long userId, Date firstDate, Date lastDate, String acctType) {
        String sql = "select sum(cash_out_fee) from t_user_cash_out where user_id = ? and status in ('1', '2') and create_time >= ?  and create_time <= ? and acct_type = ?";
        BigDecimal bigDecimal = Db.queryBigDecimal(sql, userId, firstDate, lastDate, acctType);

        if (null == bigDecimal){
            return BigDecimal.ZERO;
        }
        return bigDecimal;
    }

}
